package com.cdn.allmobilestore.activity;

import android.R.bool;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Process;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cdn.allmobilestore.R;
import com.cdn.allmobilestore.ui.BaseFragment;
import com.cdn.allmobilestore.ui.AdvFragment;
import com.cdn.allmobilestore.ulti.Constant;
import com.cdn.allmobilestore.ulti.DownloadInstallManager;
import com.cdn.allmobilestore.ulti.GlobalApplication;
import com.cdn.allmobilestore.ulti.Log;
import com.google.analytics.tracking.android.EasyTracker;


public class BaseActivity extends FragmentActivity  {
	  private FrameLayout mContentContainer;
	  private View mDecoratedView;
	  private Handler mHandler;
	  private View mHomeButton;
	  protected View mLogoButton;
	  private View mSearchButton;
	  protected View mShoppingStatusBar;
	  private TextView mTitle;
	  private View mTitleBarContainer;
	  private boolean mTitleBarEnable = true;
	  private View mActivityContainer;
	  private AdvFragment mActivityFragment;
	  private View mBtnCloseActivity;
	  private boolean mHomeButtonEnable = true;
	  private boolean mSearchButtonEnable = true;
	  protected static int mNumBack = 0;
	  protected ImageView closePopup;
	  protected View mTitleEditMode;
	  protected View mTitleNormalMode;
	  protected boolean iEdit = false;
	  protected TextView mTitleBarEditmode;
	  protected Button mEditFirstBtn;
	  protected Button mEditSecondBtn;
	  
	@Override                                                                                                                            
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.base_activity);
		
		this.mContentContainer = (FrameLayout) findViewById(R.id.content_container);
		this.mTitleBarContainer = findViewById(R.id.title_bar_container);
		this.mTitle = (TextView) findViewById(R.id.title_bar_title);
		this.mTitleBarEditmode = (TextView) findViewById(R.id.title);
		this.mHomeButton = findViewById(R.id.title_bar_home_image);
		this.mSearchButton = findViewById(R.id.title_bar_search_image);
	 	this.mTitleEditMode = findViewById(R.id.title_editmode);
	 	this.mEditFirstBtn 	=  (Button) findViewById(R.id.button1);
	 	this.mEditSecondBtn 	=  (Button) findViewById(R.id.button2);
	 	this.mLogoButton		= findViewById(R.id.title_bar_home_logo);
	 	this.mLogoButton.setVisibility(View.VISIBLE);
		this.mSearchButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(BaseActivity.this,SearchActivity.class);
				BaseActivity.this.startActivity(intent);
			}
		});
		
		this.mActivityFragment = ((AdvFragment)getSupportFragmentManager().findFragmentById(R.id.activity_fragment));
		this.mActivityContainer = findViewById(R.id.activity_container);
		this.mBtnCloseActivity = (ImageView)findViewById(R.id.close_activity);
		this.mBtnCloseActivity.setVisibility(View.GONE);
		/*this.mBtnCloseActivity.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				BaseActivity.this.mActivityContainer.setVisibility(View.GONE);
			}
		});
		*/
		DownloadInstallManager.getManager().mContext =  this;
				
		
		/*if(GlobalApplication._().isFirstTime(true))
		{
			this.mActivityFragment.loadUrl(Constant.TUTORIAL_URL);
		}
		else
		{
			this.mActivityContainer.setVisibility(View.GONE);
		}*/
		
		//tạm thời tắt popup
		
	
		//this.mActivityFragment. 
		/*this.mActivityContainer = findViewById(R.id.activity_container);
		this.mActivityFragment  = findViewById(R.id.activity_fragment);
		this.mBtnCloseActivity  = findViewById(R.id.close_activity);
		*/
		
		Log.v("activity:" + this.getClass().getSimpleName());
	}
	
	public void setEditMode(boolean isEdit)
	{
		this.iEdit = isEdit;
		if(isEdit)
		{
			this.mTitleEditMode.setVisibility(View.VISIBLE);
			this.mTitle.setVisibility(View.GONE);			
		}
		else
		{
			this.mTitleEditMode.setVisibility(View.GONE);
			this.mTitle.setVisibility(View.VISIBLE);
		}
		
	}
	
	public void setEditFirstButtonListener(View.OnClickListener listener)
	{
		this.mEditFirstBtn.setOnClickListener(listener);
	}
	
	public void setEditSecondButtonListener(View.OnClickListener listener)
	{
		this.mEditSecondBtn.setOnClickListener(listener);
	}
	
	public void setBarTitle(CharSequence title)
	{
		if(iEdit)
			this.mTitleBarEditmode.setText(title);
		else
			this.mTitle.setText(title);
	}
	protected void setCustomContentByView(View fragment)
	{
		if (this.mDecoratedView != null)
	        this.mContentContainer.removeView(this.mDecoratedView);
	      this.mContentContainer.addView(fragment);
	      this.mDecoratedView = fragment;
	}
	
	protected void setCustomContentView(int id)
	{
	    View view = View.inflate(this, id, null);
	    if (view != null)
	    {
	      if (this.mDecoratedView != null)
	        this.mContentContainer.removeView(this.mDecoratedView);
	      this.mContentContainer.addView(view);
	      this.mDecoratedView = view;
	    }
	    else
	    {
	    	if (this.mDecoratedView != null)
	    	{
		      this.mContentContainer.removeView(this.mDecoratedView);
		      this.mDecoratedView = null;
		    }
	    }	 
	}
	
	
	public AdvFragment getAdvFragment(){
		return this.mActivityFragment;
	}
	
	public void setDisplayPopUp(boolean show)
	{
		if(show)
			BaseActivity.this.mActivityContainer.setVisibility(View.VISIBLE);
		else
			BaseActivity.this.mActivityContainer.setVisibility(View.GONE);
	}
	
	public void setDisplayShowCustomEnabled(boolean show){
		if(show)
			this.mTitleBarContainer.setVisibility(View.GONE);			
	}
	
	public View getHomeButton()
	{
	  return this.mHomeButton;
	}
	
	public void setSearchButtonEnable(boolean isEnable)
	{
		this.mSearchButtonEnable = true;
		 View localView = this.mSearchButton;
	    if (this.mSearchButtonEnable)   
	      localView.setVisibility(View.VISIBLE);
	    else
	    	localView.setVisibility(View.GONE);
	}
	
	public void setHomeButtonEnable(boolean isEnable)
	 {
		
	    this.mHomeButtonEnable = isEnable;
	    View localView = this.mHomeButton;
	    if (this.mHomeButtonEnable) 
	    {
	    	localView.setVisibility(View.VISIBLE);
	    	this.mLogoButton.setVisibility(View.GONE);
	    	localView.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					BaseActivity.this.onBackPressed();
					
				}
			});
	    }	      
	    else
	    {
	    	this.mLogoButton.setVisibility(View.VISIBLE);
	    	localView.setVisibility(View.GONE);
	    }
	 
	  }
	
	protected Fragment newFragmentByTag(String tag)
	{
	    return null;
	}
	public void showFragment(String tag, Bundle bundle, boolean addToBackStack)
	  {
		if(this.mDecoratedView == null)
		{
			this.mDecoratedView = new LinearLayout(this);			
			this.mDecoratedView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
			this.mDecoratedView.setId(0x101);
		}
		
		  FragmentManager fragmentManager = getSupportFragmentManager();
		  FragmentTransaction fragmentTransition = fragmentManager.beginTransaction();
		  Fragment fragment = fragmentManager.findFragmentByTag(tag);
		  if (fragment == null)
		  {
		    fragment = newFragmentByTag(tag);
		    if (bundle != null)
		      fragment.setArguments(bundle);
		  }
		  if (fragment == null)
		  {
		    Log.v("BaseActivity NO fragment found by tag: " + tag);
		  }
		  else
		  {
		    fragmentTransition.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
		    fragmentTransition.add(this.mDecoratedView.getId(), fragment, tag);
		    if (addToBackStack)
		      fragmentTransition.addToBackStack(null);
		    fragmentTransition.commitAllowingStateLoss();
		  }
		     
		  setCustomContentByView(this.mDecoratedView);
	    
	  }
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		 EasyTracker.getInstance().activityStop(this); 
	}
	
	
	
	/* (non-Javadoc)
	 * @see android.support.v4.app.FragmentActivity#onStart()
	 */
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		EasyTracker.getInstance().activityStart(this);
	}


	@Override
	public void onBackPressed() {			
		super.onBackPressed();
	
		BaseActivity.this.overridePendingTransition(R.anim.appear, R.anim.slide_out_right);		
		if(mNumBack >= 1 )
		{
			Process.killProcess(Process.myPid());
		}
				
	
	}
	
  
	  
	  
	  
}
